package dfl.bysj.controller;


import dfl.bysj.config.RedisKey;
import dfl.bysj.entity.Academy;
import dfl.bysj.entity.Grade;
import dfl.bysj.entity.VO.StudentVO;
import dfl.bysj.entity.VO.TeacherVO;
import dfl.bysj.entity.VO.UpdateBindVO;
import dfl.bysj.entity.VO.UpdateInformationVO;
import dfl.bysj.exception.UnifiedException;
import dfl.bysj.handler.NoAuth;
import dfl.bysj.result.R;
import dfl.bysj.result.ResponseEnum;
import dfl.bysj.service.AcademyService;
import dfl.bysj.service.GradeService;
import dfl.bysj.service.TeacherService;
import dfl.bysj.service.UpdateBindService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DFL
 * @since 2022-04-26
 */
@Slf4j
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    TeacherService teacherService;

    private RedisTemplate redisTemplate;

    private GradeService gradeService;

    private AcademyService academyService;

    @Autowired
    public void setGradeService(GradeService gradeService) {
        this.gradeService = gradeService;
    }

    @Autowired
    public void setAcademyService(AcademyService academyService) {
        this.academyService = academyService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Autowired
    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @NoAuth
    @ApiOperation("注册")
    @PostMapping("/regist")
    public R regist(TeacherVO teacherVO){
        /**
         * 通过获取StudentVo的String,判断是否有null或空值
         */

        String checkString=teacherVO.toString();
        log.info(checkString);
        if (checkString.contains("null")){
            throw new UnifiedException(ResponseEnum.STUDENT_TEACHER_INFO_NOT_OVER);
        }

        return R.ok().data("registTea",teacherService.regist(teacherVO));
    }

    /**
     * 改绑审核
     * 1、显示所有改绑申请
     */
    @GetMapping("/show_update_bind")
    public R show_update_bind(){

        ArrayList<UpdateBindVO> updateBindVOS=teacherService.getAllUpdateBindVO();

        return R.ok().data("resultList",updateBindVOS);
    }


    /**
     * 获取grade和academy根据updateBindVO
     * @param updateBindVO
     * @return
     */
    @GetMapping("/get_update_bind_grade_academy")
    public R get_update_bind_grade_academy(Integer before_graId,Integer after_graId){
        log.info(before_graId.toString());
        HashMap<String,Object> result=new HashMap();
        Grade before_grade=gradeService.getGradeById(before_graId);
        Grade after_grade=gradeService.getGradeById(after_graId);

        Academy before_academy=academyService.getAcademyById(before_grade.getAcaId());
        Academy after_academy=academyService.getAcademyById(after_grade.getAcaId());

        result.put("before_grade",before_grade);
        result.put("after_grade",after_grade);
        result.put("before_academy",before_academy);
        result.put("after_academy",after_academy);

        return R.ok().data("result",result);
    }


    /**
     * 通过审核
     * 1、通过useid获取修改表
     * 2、删除表
     * 3、修改关系表中的绑定
     * @param use_id
     * @return
     */
    @GetMapping("/pass_check")
    public R pass_check(String use_id){
        teacherService.passCheck(Integer.parseInt(use_id));
        return R.ok();
    }

    @GetMapping("/deny_check")
    public R deny_check(String use_id){
        teacherService.denyCheck(Integer.parseInt(use_id));
        return R.ok();
    }

    /**
     * 信息修改显示
     * 1、从数据库获取所有内容
     * 2、
     * @return
     */
    @GetMapping("/show_update_information")
    public R show_update_information(){

        ArrayList<UpdateInformationVO> updateInformationVOS=teacherService.getAllUpdateInformationVO();
        log.info(updateInformationVOS.toString());
        return R.ok().data("resultList",updateInformationVOS);
    }

    @GetMapping("/pass_check_information")
    public R pass_check_information(String use_id){
        teacherService.passCheckInformation(Integer.parseInt(use_id));
        return R.ok();
    }

    @GetMapping("/deny_check_information")
    public R deny_check_information(String use_id){
        teacherService.denyCheckInformation(Integer.parseInt(use_id));
        return R.ok();
    }
}

